package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.O2oAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.O2oModel;
import com.banshuibao.bsb_android.model.SearchRequestModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.KeyBroadHintShow;
import com.banshuibao.bsb_android.util.XToast;

import java.util.List;

public class SearchPersonActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2, View.OnClickListener {

    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private int startId, endId, rows;
    private boolean isUp;
    private O2oAdapter adapter;
    private EditText et;
    private TextView menu;
    private String content;
    private ImageView empty_iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_person);
        initView();
    }

    public void initView() {
        et = findViewById(R.id.et);
        empty_iv = findViewById(R.id.empty);
        menu = findViewById(R.id.menu);
        menu.setOnClickListener(this);
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        adapter = new O2oAdapter(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(adapter !=null){
                    String uuid = adapter.getItem(position - 1).getUuid();
                    IntentManager.goPersonPageActivity(SearchPersonActivity.this,uuid);
                }
            }
        });
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length()==0){
                    empty_iv.setVisibility(View.VISIBLE);
                    pullToRefreshListView.setVisibility(View.GONE);
                    if(adapter!=null){
                        adapter.clear();
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        };
        et.addTextChangedListener(textWatcher);
    }

    public void setData() {
        isUp = false;
        startId = 1;
        endId = startId + 9;
        if (adapter != null && adapter.getCount() > 10) {
            endId = adapter.getCount();
        }
        requestNet(content);
    }

    public void requestNet(String name){
        SearchRequestModel searchRequestModel = new SearchRequestModel();
        searchRequestModel.setSinceId(startId);
        searchRequestModel.setMaxId(endId);
        searchRequestModel.setName(name);
        HttpUtil.doPost(Constants.Url.SEARCH_TAX, searchRequestModel, new HttpResponse(this, O2oModel.class) {
            @Override
            public void onResult(Object obj) {
                O2oModel model = (O2oModel) obj;
                List<O2oModel.DataBean> data = model.getData();
                rows = model.getRows();
                startId += rows;
                if(isUp){
                    adapter.setUpData(data);
                }else{
                    adapter.setDownData(data);
                }
                if(adapter != null && adapter.getCount()>0){
                    empty_iv.setVisibility(View.GONE);
                    pullToRefreshListView.setVisibility(View.VISIBLE);
                }else{
                    empty_iv.setVisibility(View.VISIBLE);
                    pullToRefreshListView.setVisibility(View.GONE);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        setData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            isUp = true;
            endId = startId + 9;
            requestNet(content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menu:
                content = et.getText().toString().trim();
                if(!TextUtils.isEmpty(content)){
                    KeyBroadHintShow.hintKeyboard(SearchPersonActivity.this);
                    setData();
                }else{
                    XToast.showToast(this,"请输入专家姓名！");
                }
                break;
        }
    }
}
